package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.BargainGroupService;
import com.ymatou.iapi.refund.parameter.CreateReturnBillBean;
import com.ymatou.iapi.refund.parameter.ProductCatalogModel;
import com.ymatou.iapi.refund.service.CreateReturnBillCall;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.TestDataManager;

public class RefundCallService {

	private static YmtCatalogsIWapper cwapper=new YmtCatalogsIWapper();
	
	/**
	 * 一个商家，一个商品，一个订单 已接单
	 * @param isBargainGroup 是否是砍价团    是否砍价团订单 砍价团订单不由交易扣库存
	 * @param isUserCoupon 是否使用优惠券
	 * @return
	 * @throws InterruptedException 
	 */
	public  CreateReturnBillBean setCreateReturnBillBean_AcceptOrder(
			TradingServiceResp tradingServiceResp,TradingPlaceOrderCallService tradingService,boolean isBargainGroup) throws InterruptedException
	{
      	String num=TestDataManager.getRandomNum(5);
		List<Order> orders=tradingServiceResp.getOrderList();
		CreateReturnBillBean bean=new CreateReturnBillBean();
		
		Order order=tradingServiceResp.getOrderList().get(0);
		long orderId=order.getOrderId();
        int userId=(int)tradingServiceResp.getUserId();
        
        BigDecimal returnAmount=tradingServiceResp.getPayableAmount();
        Logger.comment("##orderId##:"+orderId);
        
		String 	catalogId=orders.get(0).getOrderDetailPoList().get(0).getCatalogId();
		int returnCount=order.getOrderDetailPoList().get(0).getProductCount();
		//订单支付
		TradingCallService.payOrder(userId,returnAmount,tradingServiceResp.getTradingId());
		
		bean.setOrderId(orderId);
		bean.setReturnAmount(returnAmount);
		bean.setReturnRemark("自动化测试");
		bean.setReturnReasonId(1);
		bean.setReturnType(0);
		bean.setIsRecevied(true);
		bean.setUserid(userId);
		bean.setIsYMTHandle(false);
		String pic="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
		List<String> pics=new ArrayList();
		pics.add(pic);
		bean.setPicUrls(pics);
		
		ProductCatalogModel productModel=new ProductCatalogModel();
		productModel.setCatalogId(catalogId);
		productModel.setReturnCount(returnCount);
		List<ProductCatalogModel> products = new ArrayList<ProductCatalogModel>();
		products.add(productModel);
		bean.setProducts(products);

		if(isBargainGroup==true)
		{
			BargainGroupService bargainGroupService=new BargainGroupService();
			bargainGroupService.notifyOrderBizStatus(tradingServiceResp.getBizId(), orderId, 1, 3);
		}
		else
		{
		  //商家接单
		  YmtCatalogsI catalog=cwapper.selectCatalogsByCatalogId(bean.getProducts().get(0).getCatalogId());
		  TradingCallService.sellerAcceptOrder(order.getSellerId(),orderId);
		}
		
		return bean;
	}
	
	
	/**
	 * 1个买手,多商品,生成2个订单   已接单
	 * @param isBargainGroup 是否是砍价团
	 * @param isUserCoupon 是否使用优惠券
	 * @return
	 * @throws InterruptedException 
	 */
	public static CreateReturnBillBean setCreateReturnBillMoreBean_AcceptOrder(
			TradingServiceResp tradingServiceResp,TradingPlaceOrderCallService tradingService) throws InterruptedException
	{
		String num=TestDataManager.getRandomNum(5);
		CreateReturnBillBean bean=new CreateReturnBillBean();
		
		Order order=tradingServiceResp.getOrderList().get(0);
		
        long orderId=order.getOrderId();
        int userId=(int)tradingServiceResp.getUserId();
        BigDecimal payAmount=tradingServiceResp.getPayableAmount();
        
        Logger.comment("##orderId##:"+orderId);
        Logger.comment("##payAmount##:"+payAmount);
        
        String 	catalogId=order.getOrderDetailPoList().get(0).getCatalogId();
		int returnCount=order.getOrderDetailPoList().get(0).getProductCount();
		
//		String 	catalogId1=order.getOrderDetailPoList().get(1).getCatalogId();
//		int returnCount1=order.getOrderDetailPoList().get(1).getProductCount();
		
		
		//订单支付
		TradingCallService.payOrder(userId,payAmount,tradingServiceResp.getTradingId());
		
		bean.setOrderId(orderId);
		bean.setReturnAmount(order.getOrderDetailPoList().get(0).getProductPrice());
		bean.setReturnRemark("自动化测试申请退货单_"+num);
		bean.setReturnReasonId(137);
		bean.setReturnType(0);
		bean.setIsRecevied(true);
		bean.setUserid(userId);
		bean.setIsYMTHandle(false);
		String pic="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
		List<String> pics=new ArrayList();
		pics.add(pic);
		bean.setPicUrls(pics);
		
		ProductCatalogModel productModel=new ProductCatalogModel();
		productModel.setCatalogId(catalogId);
		productModel.setReturnCount(returnCount);
		
//		ProductCatalogModel productModel1=new ProductCatalogModel();
//		productModel1.setCatalogId(catalogId1);
//		productModel1.setReturnCount(returnCount1);
		
		List<ProductCatalogModel> products = new ArrayList<ProductCatalogModel>();
		products.add(productModel);
		
		bean.setProducts(products);
		
		//商家接单
		YmtCatalogsI catalog=cwapper.selectCatalogsByCatalogId(bean.getProducts().get(0).getCatalogId());
		TradingCallService.sellerAcceptOrder(catalog.getiUserId(),orderId);

		return bean;
	}
	
	/**
	 * 一个商家，一个商品，一个订单 未接单
	 * @param isBargainGroup 是否是砍价团    是否砍价团订单 砍价团订单不由交易扣库存
	 * @param isUserCoupon 是否使用优惠券
	 * @return
	 * @throws Exception 
	 */
	public static  CreateReturnBillBean setCreateReturnBillBean(
			TradingServiceResp tradingServiceResp,TradingPlaceOrderCallService tradingService) throws Exception
	{
      	String num=TestDataManager.getRandomNum(5);
		List<Order> orders=tradingServiceResp.getOrderList();
		CreateReturnBillBean bean=new CreateReturnBillBean();
		
        long orderId=orders.get(0).getOrderId();
        int userId=(int)tradingServiceResp.getUserId();
        BigDecimal returnAmount=tradingServiceResp.getPayableAmount();
        Logger.comment("##orderId##:"+orderId);
        
		String 	catalogId=orders.get(0).getOrderDetailPoList().get(0).getCatalogId();
		int returnCount=orders.get(0).getOrderDetailPoList().get(0).getProductCount();
		//订单支付
		TradingCallService.payOrder(userId,returnAmount,tradingServiceResp.getTradingId());
				
		BigDecimal totalPrice = returnAmount;

//		Long thirdTradingId = TradingCallService.payOrder(userId,
//				BigDecimal.ZERO, tradingServiceResp.getTradingId(), "13");
//
//		PayGateWayCallService.notifyCall("13", thirdTradingId.toString(),
//				totalPrice.subtract(BigDecimal.ZERO).toString(),
//				userId);
		//TradingSyncService.getSyncResult(orderId);

		
		bean.setOrderId(orderId);
		//bean.setReturnAmount(returnAmount);
		bean.setReturnAmount(tradingServiceResp.getOrderList().get(0).getOrderPayableAmount());
		
		bean.setReturnRemark("自动化测试");
		bean.setReturnReasonId(1);
		bean.setReturnType(0);
		bean.setIsRecevied(true);
		bean.setUserid(userId);
		bean.setIsYMTHandle(false);
		String pic="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
		List<String> pics=new ArrayList();
		pics.add(pic);
		bean.setPicUrls(pics);
		
		ProductCatalogModel productModel=new ProductCatalogModel();
		productModel.setCatalogId(catalogId);
		productModel.setReturnCount(returnCount);
		List<ProductCatalogModel> products = new ArrayList<ProductCatalogModel>();
		products.add(productModel);
		bean.setProducts(products);
		
		return bean;
	}
	
	
	/**
	 * 1个买手,多商品,生成2个订单   未接单
	 * @param isBargainGroup 是否是砍价团
	 * @param isUserCoupon 是否使用优惠券
	 * @return
	 * @throws InterruptedException 
	 */
	public static CreateReturnBillBean setCreateReturnBillMoreBean(
			TradingServiceResp tradingServiceResp,TradingCallService tradingService) throws InterruptedException
	{
		String num=TestDataManager.getRandomNum(5);
		CreateReturnBillBean bean=new CreateReturnBillBean();
		
		Order order=tradingServiceResp.getOrderList().get(0);
		
        int orderId=(int)order.getOrderId();
        int userId=(int)tradingServiceResp.getUserId();
        BigDecimal payAmount=tradingServiceResp.getPayableAmount();
        
        Logger.comment("##orderId##:"+orderId);
        Logger.comment("##payAmount##:"+payAmount);
        
		String 	catalogId1=order.getOrderDetailPoList().get(0).getCatalogId();
		int returnCount1=order.getOrderDetailPoList().get(0).getProductCount();
		
		//订单支付
		TradingCallService.payOrder(userId,payAmount,tradingServiceResp.getTradingId());
		
		bean.setOrderId(orderId);
		bean.setReturnAmount(order.getOrderDetailPoList().get(0).getProductPrice());
		bean.setReturnRemark("自动化测试申请退货单_"+num);
		bean.setReturnReasonId(137);
		bean.setReturnType(0);
		bean.setIsRecevied(true);
		bean.setUserid(userId);
		bean.setIsYMTHandle(false);
		String pic="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
		List<String> pics=new ArrayList();
		pics.add(pic);
		bean.setPicUrls(pics);
		
		ProductCatalogModel productModel=new ProductCatalogModel();

		productModel.setCatalogId(catalogId1);
		productModel.setReturnCount(returnCount1);
		
		List<ProductCatalogModel> products = new ArrayList<ProductCatalogModel>();
		products.add(productModel);
		bean.setProducts(products);

		return bean;
	}
	
	public static CreateReturnBillCall callCreateReturnBill(CreateReturnBillBean bean,CreateReturnBillCall call) throws ClientProtocolException, IOException, InterruptedException
	{
		Thread.sleep(3000);
		call.setData(bean);
		call.callService();
		return call;
	}
	
	 
}
